<?=$this->header()?>

    <h1><?=$this->message("home.header")?></h1>

	<?php
	
	// ----------------------------------------------------- Instance Variables
	
	// Total number of rows
	$results = 200;
	
	// Number of rows per page
	$limit = 10;
	
	// Current page (starting with 1)
	$page = isset($_GET["p"]) ? intval($_GET["p"]) : 1;
	
	// Number of number click links (Must be even number!)
	$links = 10;
	
	// Half of the links ( for each side )
	$half = round($links / 2);
	
	
	// -------------------------------------------------- Calculate result info
	
	// calculate pages
	$pages = ceil($results / $limit);
	
	// calculate start 
	$start = ($page - 1) * $limit + 1;
	
	// calculate end
	$end = ($start - 1) + $limit > $results ? $results : ($start - 1) + $limit;
	
	
	// ---------------------------------------------------- Calculate link info
	
	if($pages > $links) {
	
	    // If page > half of #links before the end
	    // then start showing more links on the left hand side and keep
	    // the total on 10 ($links)
	    if($page > $pages - $half) {
	        $linkstart = $page - ($links - ($pages - $page)) + 1;
	    // if page > half way -> start taking off links on the left hand side.
	    } else if($page > $half) {
	        $linkstart = $page - $half + 1;
	    // normal start
	    } else {
	        $linkstart = 1;
	    }
	
	    // if page + (half of links) > total pages -> end on pages
	    if($page + $half > $pages) {
	        $linkend = $pages;
	    // page is not yet halfway -> end of #links
	    } else if($page <= $half) {
	        $linkend = $links;
	    // else end at page + half of #links
	    } else {
	        $linkend = $page + $half;
	    }
	}
	
	
	// -------------------------------------------------------------- Show HTML
	
	?>
	
	<div style="width:auto;text-align:center;">
	
	Showing: <?=$start?> to <?=$end?> | Total: <?=$results?>
	
	<table style="margin-left:auto;margin-right:auto;">
	<tr>
	    <? if(isset($linkstart)): ?>
	
	        <td><?=$page > 1 ? "<a href=\"home?p=1\">start</a>" : "start"?></td>
	        <td><?=$page > 1 ? "<a href=\"home?p=".($page - 1)."\">previous</a>" : "previous"?></td>
	
	        <?for($i=$linkstart; $i<=$linkend; $i++):?>
	
	            <td style="width:24px;text-align:center;"><a href="home?p=<?=$i?>"><?=$i == $page ? "<b>".$i."</b>" : $i?></a></td>
	
	        <?endfor;?>
	
	        <td><?=$page < $pages ? "<a href=\"home?p=".($page + 1)."\">next</a>" : "next"?></td>
	        <td><?=$page < $pages ? "<a href=\"home?p=".$pages."\">end</a>" : "end"?></td>
	
	    <? else: ?>
	
	        <?for($i=1; $i<=$pages; $i++):?>
	
	            <td style="width:24px;text-align:center;"><a href="?p=<?=$i?>"><?=$i == $page ? "<b>".$i."</b>" : $i?></a></td>
	
	        <?endfor;?>
	    
	    <? endif; ?>
	
	</tr>
	</table>
	
	</div>

<?=$this->footer()?>